<template>
	<Form :model="formData">
		<FormItem label="岗位名" label-position="top">
			<Input v-model="formData.name" placeholder="请输入岗位名称" />
		</FormItem>
		
		<FormItem label="岗位描述" label-position="top">
			<Input type="textarea" v-model="formData.description" :rows="4" placeholder="请输入岗位描述信息" />
		</FormItem>
		
		<FormItem label="状态" label-position="top">
		  <Switch size="large" v-model="formData.status">
			<template #open>
			  <span>启用</span>
			</template>
			<template #close>
			  <span>禁用</span>
			</template>
		  </Switch>
		</FormItem>
	</Form>	
	
	<div class="demo-drawer-footer">
		<Button style="margin-right: 8px" @click="$emit('close-win', false)">关闭</Button>
		<Button type="primary" @click="saveOrUpdate">保存</Button>
	</div>
</template>

<script>
export default {
  props: {
    row: {
      type: Object,
      required: false,
      default: {}
    }
  },
	data() {
		return {
			formData: {
				name: this.row.name,
				description: this.row.description,
				status: this.row.status ?? true
			},
		}
	},
	emits: ["close-win"],
  methods: {
    saveOrUpdate() {
      // 验证参数
      if (this.formData.name == null || this.formData.name === "") {
        this.$Message.error({background: true, content: "请输入岗位名称"});
        return false;
      }

      let param = {name: this.formData.name, description: this.formData.description, status: this.formData.status};
      if (this.row.id == null) {
        // 添加 岗位
        this.$api.addPosition(param).then(({data:ret})=> {
           if (ret.status === "10000") {
             this.$emit('close-win' , true) ;
             this.$Message.success({background: true, content:'数据添加成功'});
           }else{
             this.$Message.error({background: true, content:'数据添加失败'});
           }
        })
      } else{ // 修改岗位
        param["id"] = this.row.id ;
        this.$api.modifyPosition(param).then(({data:ret})=> {
          if (ret.status === "10000") {
            this.$emit('close-win' , true) ;
            this.$Message.success({background: true, content:'数据修改成功'});
          }else{
            this.$Message.error({background: true, content:'数据添加失败'});
          }
        })
      }
    }
  }
}
</script>

<style scoped>
.demo-drawer-footer{
	width: 100%;
	position: absolute;
	bottom: 0;
	left: 0;
	border-top: 1px solid #e8e8e8;
	padding: 10px 16px;
	text-align: right;
	background: #fff;
}
</style>